home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
Tickle-4.0 (tcl)
/
src
/
asdtomac.c
< prev
next >
Wrap
Text File
|
1993-11-18
|
14KB
|
616 lines
/*
** This source code was written by Tim Endres
** Email: time@ice.com.
** USMail: 8840 Main Street, Whitmore Lake, MI 48189
**
** Some portions of this application utilize sources
** that are copyrighted by ICE Engineering, Inc., and
** ICE Engineering retains all rights to those sources.
**
** Neither ICE Engineering, Inc., nor Tim Endres,
** warrants this source code for any reason, and neither
** party assumes any responsbility for the use of these
** sources, libraries, or applications. The user of these
** sources and binaries assumes all responsbilities for
** any resulting consequences.
*/
#ifdef MPW3
# pragma segment ASD
#endif
#ifndef THINK_C_PRECOMPILED
#include <types.h>
#include <resources.h>
#include <toolutils.h>
#include <standardfile.h>
#include <errors.h>
#include <fcntl.h>
#include <stdio.h>
#include "defines.h"
#endif /*THINK_C_PRECOMPILED*/
#include "asd.h"
#include "mb.h"
#include <errno.h>
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
#undef BLK_SIZE
#define BLK_SIZE 1024
#define iobuffer kBuffer
extern char kBuffer[];
/* static char iobuffer[BLK_SIZE]; */
#ifdef TCLAPPL
SF_asd_to_Mac()
{
Point mypoint;
SFReply asdreply;
SFReply macreply;
SFTypeList mytypes;
FILE *asdfile;
char name[64];
mypoint.h = mypoint.v = 75;
mytypes[0] = 'TEXT';
MyGetFile(mypoint, "\p", NULL, (CheckOption()?-1:1), mytypes, NULL, &asdreply);
if (asdreply.good) {
p2cstr(asdreply.fName);
Feedback("Apple Single/Double to Macintosh - '%s'", asdreply.fName);
SetVol(NULL, asdreply.vRefNum);
asdfile = fopen(asdreply.fName, "r");
if (asdfile == NULL)
{
Feedback("Error #%d opening Apple Single/Double file '%s'",
errno, asdreply.fName);
return FAILURE;
}
sprintf(name, "%s.mac", asdreply.fName);
asd_seek_a_name(asdfile, name);
c2pstr(name);
MyPutFile(mypoint, "\pMacintosh File:", name, NULL, ¯eply);
yield_menus(YIELD_ON);
do_asd_to_mac(asdreply.fName, asdfile, macreply.fName, macreply.vRefNum, TRUE);
WatchCursorOn();
fclose(asdfile);
yield_menus(YIELD_OFF);
UInitCursor();
}
}
#endif
do_asd_to_mac(inname, infile, mac_fName, mac_vRefNum, interactive)
char *inname; /* C */
FILE *infile;
char *mac_fName; /* Pascal */
int mac_vRefNum;
int interactive;
{
int result = noErr;
long bytes;
bytes = fread(&asd_header, 1, sizeof(asd_hdr), infile);
if (bytes != sizeof(asd_hdr))
{
Feedback("Error #%d reading Apple Single/Double header bytes.", errno);
result = FAILURE;
}
else switch (asd_header.magic)
{
case MAGIC_APPLE_SINGLE: /* AppleSingle */
if (interactive)
Feedback("Apple Single -> Macintosh.");
result = apple_single_to_mac(inname, infile,
mac_fName, mac_vRefNum, &asd_header, interactive);
break;
case MAGIC_APPLE_DOUBLE: /* AppleDouble */
if (interactive)
Feedback("Apple Double -> Macintosh.");
result = apple_double_to_mac(inname, infile,
mac_fName, mac_vRefNum, &asd_header, interactive);
break;
default:
if (interactive)
Feedback("Unknown magic number 0x%08lx. Not processed.",
asd_header.magic);
result = FAILURE;
break;
}
return result;
}
apple_single_to_mac(inname, infile, mac_fName, mac_vRefNum, header, interactive)
char *inname; /* C */
FILE *infile;
char *mac_fName; /* Pascal */
int mac_vRefNum;
asd_hdr *header;
int interactive;
{
int i, myerr;
int got_data, got_rsrc, got_fndr;
int i_data, i_rsrc, i_fndr;
ParamBlockRec pb;
FInfo finfo;
#pragma unused (inname, outname)
got_data = got_rsrc = got_fndr = 0;
#ifdef TCLAPPL
print_asd_hdr(header);
#endif
RotateCursor(32);
DoYield();
for (i=0; i < header->entries; i++)
{
fread(&the_entries[i], 1, sizeof(asd_entry), infile);
#ifdef TCLAPPL
print_asd_entry(&the_entries[i]);
#endif
switch (the_entries[i].id)
{
case ASDID_DATA_FORK: got_data = 1; i_data = i; break;
case ASDID_RSRC_FORK: got_rsrc = 1; i_rsrc = i; break;
case ASDID_FNDR_FORK: got_fndr = 1; i_fndr = i; break;
}
}
RotateCursor(32);
DoYield();
if (! got_fndr)
{
if (interactive)
Feedback("NOTE: No Finder Information. Defaulting.");
finfo.fdType = (OSType)'????';
finfo.fdCreator = (OSType)'????';
finfo.fdFlags = 0;
finfo.fdFldr = 0;
finfo.fdLocation.h = 16;
finfo.fdLocation.v = 16;
}
else {
get_asd_fndr_data(infile, &the_entries[i_fndr], &g_asd_finder);
#ifdef TCLAPPL
print_asd_fndr(&g_asd_finder);
#endif
finfo.fdType = g_asd_finder.fdType;
finfo.fdCreator = g_asd_finder.fdCreator;
finfo.fdFlags = g_asd_finder.fdFlags;
memcpy(&finfo.fdLocation, g_asd_finder.fdLocation, sizeof(finfo.fdLocation));
}
if (interactive)
{
Feedback("DATA Fork contains %ld bytes.", the_entries[i_data].length);
Feedback("RSRC Fork contains %ld bytes.", the_entries[i_rsrc].length);
}
RotateCursor(32);
DoYield();
myerr = Create(mac_fName, (short)mac_vRefNum, finfo.fdCreator, finfo.fdType);
if (myerr != noErr && myerr != dupFNErr && interactive)
Feedback("Error #%d creating '%.*s'.", myerr, mac_fName[0], &mac_fName[1]);
pb.ioParam.ioNamePtr = mac_fName;
pb.ioParam.ioVRefNum = mac_vRefNum;
if (got_data && the_entries[i_data].length > 0)
{
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsWrPerm;
pb.ioParam.ioMisc = 0;
myerr = PBOpen(&pb, FALSE);
if (myerr != noErr)
{
if (interactive)
Feedback("Error #%d opening DATA fork '%.*s' for writing.",
myerr, mac_fName[0], &mac_fName[1]);
}
else {
if (interactive)
Feedback("Writing DATA fork...");
do_mac_copy( infile, the_entries[i_data].offset,
pb.ioParam.ioRefNum, the_entries[i_data].length );
pb.ioParam.ioCompletion = 0;
myerr = PBClose(&pb, FALSE);
}
}
RotateCursor(32);
DoYield();
if (got_rsrc && the_entries[i_rsrc].length)
{
SetVol(NULL, (short)mac_vRefNum);
CreateResFile(mac_fName);
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsWrPerm;
pb.ioParam.ioMisc = 0;
myerr = PBOpenRF(&pb, FALSE);
if (myerr != noErr)
{
if (interactive)
Feedback("Error #%d opening RSRC fork '%.*s' for writing.",
myerr, mac_fName[0], &mac_fName[1]);
}
else {
if (interactive)
Feedback("Writing RSRC fork...");
do_mac_copy( infile, the_entries[i_rsrc].offset,
pb.ioParam.ioRefNum, the_entries[i_rsrc].length );
pb.ioParam.ioCompletion = 0;
myerr = PBClose(&pb, FALSE);
}
}
RotateCursor(32);
DoYield();
pb.fileParam.ioCompletion = 0;
pb.fileParam.ioFVersNum = 0;
pb.fileParam.ioFlFndrInfo = finfo;
pb.fileParam.ioFlCrDat = 0;
pb.fileParam.ioFlMdDat = 0;
myerr = PBSetFInfo(&pb, FALSE);
if (myerr != noErr && interactive)
{
Feedback("Warning #%d setting '%.*s' Finder info.",
myerr, mac_fName[0], &mac_fName[1]);
}
return noErr;
}
apple_double_to_mac(rname, rfile, mac_fName, mac_vRefNum, header, interactive)
char *rname; /* C */
FILE *rfile;
char *mac_fName; /* Pascal */
int mac_vRefNum;
asd_hdr *header;
int interactive;
{
FILE *dfile = NULL;
SFReply myreply;
Point mypoint;
SFTypeList mytypes;
ParamBlockRec pb;
FInfo finfo;
int i, myerr;
int got_data, got_rsrc, got_fndr, got_path;
int i_data = 0, i_rsrc, i_fndr, i_path;
long bytes;
char dname[256];
#pragma unused (outname)
got_data = got_rsrc = got_fndr = 0;
#ifdef TCLAPPL
print_asd_hdr(header);
#endif
the_entries[i_data].id = ASDID_DATA_FORK;
the_entries[i_data].offset = 0;
the_entries[i_data].length = 0;
RotateCursor(32);
DoYield();
for (i=1; i <= header->entries; i++)
{
fread(&the_entries[i], 1, sizeof(asd_entry), rfile);
#ifdef TCLAPPL
print_asd_entry(&the_entries[i]);
#endif
switch (the_entries[i].id) {
case ASDID_RSRC_FORK: got_rsrc = 1; i_rsrc = i; break;
case ASDID_FNDR_FORK: got_fndr = 1; i_fndr = i; break;
case ASDID_DATA_PATH: got_path = 1; i_path = i; break;
}
}
RotateCursor(32);
DoYield();
if (got_path)
{
if (the_entries[i_path].length >= sizeof(dname)-1)
{
if (interactive)
Feedback("Real Name Too Long: %d > %d. Truncating.",
the_entries[i_path].length, sizeof(dname)-1);
the_entries[i_path].length = sizeof(dname) - 1;
}
bytes = fread(dname, 1, the_entries[i_path].length, rfile);
if (bytes > the_entries[i_path].length)
{
if (interactive)
Feedback("Real Name Too Long: %d > %d. Truncating to '%.*s'.",
bytes, the_entries[i_path].length,
the_entries[i_path].length, dname);
dname[the_entries[i_path].length] = '\0';
}
else if (bytes < the_entries[i_path].length)
{
if (interactive)
Feedback("Couldn't Read All Of Real Name: %d < %d. Using '%.*s'.",
bytes, sizeof(dname)-1, bytes, dname);
dname[bytes] = '\0';
}
else {
dname[bytes] = '\0';
}
if (interactive)
Feedback("Opening Real Name: '%s'.", dname);
dfile = fopen(dname, "r");
if (dfile != NULL)
got_data = 1;
}
RotateCursor(32);
DoYield();
if (! got_data)
{
if (rname[0] == '%')
{
strcpy(dname, &rname[1]);
}
else
{
sprintf(dname, "%%%s", rname);
}
if (interactive)
Feedback("Trying Data Name: '%s'.", dname);
dfile = fopen(dname, "r");
if (dfile == NULL)
{
if (! interactive)
{
got_data = 0;
}
else
{
#ifdef TCLAPPL
mypoint.h = mypoint.v = 75;
MyGetFile(mypoint, "\pWhere is data fork?", NULL, -1, mytypes, NULL, &myreply);
if (myreply.good)
{
sprintf(dname, "%.*s", myreply.fName[0], &myreply.fName[1]);
SetVol(NULL, myreply.vRefNum);
dfile = fopen(dname, "r");
if (dfile != NULL)
got_data = 1;
else
return FAILURE;
}
else {
if (! AskYesNo("Do you just want an empty data fork?", 1))
return CANCEL;
}
#else
return FAILURE;
#endif
}
}
else
got_data = 1;
}
RotateCursor(32);
DoYield();
if (dfile != NULL)
{
the_entries[i_data].offset = 0;
fseek(dfile, 0, SEEK_END);
the_entries[i_data].length = ftell(dfile);
fseek(dfile, 0, SEEK_SET);
}
else {
the_entries[i_data].offset = 0;
the_entries[i_data].length = 0;
}
if (! got_fndr)
{
if (interactive)
Feedback("NOTE: No Finder Information. Defaulting.");
finfo.fdType = (OSType)'????';
finfo.fdCreator = (OSType)'????';
finfo.fdFlags = 0;
finfo.fdFldr = 0;
finfo.fdLocation.h = 16;
finfo.fdLocation.v = 16;
}
else
{
get_asd_fndr_data(rfile, &the_entries[i_fndr], &g_asd_finder);
#ifdef TCLAPPL
print_asd_fndr(&g_asd_finder);
#endif
finfo.fdType = g_asd_finder.fdType;
finfo.fdCreator = g_asd_finder.fdCreator;
finfo.fdFlags = g_asd_finder.fdFlags;
memcpy(&finfo.fdLocation, g_asd_finder.fdLocation, sizeof(finfo.fdLocation));
}
RotateCursor(32);
DoYield();
if (interactive)
{
Feedback("DATA Fork contains %ld bytes.", the_entries[i_data].length);
Feedback("RSRC Fork contains %ld bytes.", the_entries[i_rsrc].length);
}
pb.ioParam.ioNamePtr = mac_fName;
pb.ioParam.ioVRefNum = mac_vRefNum;
myerr = Create(mac_fName, (short)mac_vRefNum, finfo.fdCreator, finfo.fdType);
if (myerr != noErr && myerr != dupFNErr && interactive)
Feedback("Error #%d creating '%.*s'.", myerr, mac_fName[0], &mac_fName[1]);
if (the_entries[i_data].length > 0)
{
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsWrPerm;
pb.ioParam.ioMisc = 0;
myerr = PBOpen(&pb, FALSE);
if (myerr != noErr)
{
if (interactive)
Feedback("Error #%d opening DATA fork '%.*s' for writing.",
myerr, mac_fName[0], &mac_fName[1]);
}
else {
if (interactive)
Feedback("Writing DATA fork...");
do_mac_copy( dfile, the_entries[i_data].offset,
pb.ioParam.ioRefNum, the_entries[i_data].length );
pb.ioParam.ioCompletion = 0;
myerr = PBClose(&pb, FALSE);
}
}
RotateCursor(32);
DoYield();
if (got_rsrc && the_entries[i_rsrc].length)
{
SetVol(NULL, (short)mac_vRefNum);
CreateResFile(mac_fName);
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioVersNum = 0;
pb.ioParam.ioPermssn = fsWrPerm;
pb.ioParam.ioMisc = 0;
myerr = PBOpenRF(&pb, FALSE);
if (myerr != noErr)
{
if (interactive)
Feedback("Error #%d opening RSRC fork '%.*s' for writing.",
myerr, mac_fName[0], &mac_fName[1]);
}
else {
if (interactive)
Feedback("Writing RSRC fork...");
do_mac_copy( rfile, the_entries[i_rsrc].offset,
pb.ioParam.ioRefNum, the_entries[i_rsrc].length );
pb.ioParam.ioCompletion = 0;
myerr = PBClose(&pb, FALSE);
}
}
pb.fileParam.ioCompletion = 0;
pb.fileParam.ioFVersNum = 0;
pb.fileParam.ioFlFndrInfo = finfo;
pb.fileParam.ioFlCrDat = 0;
pb.fileParam.ioFlMdDat = 0;
myerr = PBSetFInfo(&pb, FALSE);
if (myerr != noErr)
{
if (interactive)
Feedback("Warning #%d setting '%.*s' Finder info.",
myerr, mac_fName[0], &mac_fName[1]);
}
if (dfile)
fclose(dfile);
}
do_mac_copy(infile, inoffset, outref, length)
FILE *infile;
long inoffset;
int outref;
long length;
{
int blocks, residue, myerr;
ParamBlockRec pb;
//Feedback("do_mac_copy: in_offset = %ld outref = %d ", inoffset, outref);
if (inoffset != -1)
fseek(infile, inoffset, SEEK_SET);
//Feedback("do_mac_copy: length = %ld BLKSZ %d ", length, BLK_SIZE);
blocks = length / BLK_SIZE;
residue = length % BLK_SIZE;
//Feedback("do_mac_copy: blks = %ld res %ld", blocks, residue);
for ( ; blocks ; blocks--)
{
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
if (cancel_current_op)
return CANCEL;
fread(iobuffer, 1, BLK_SIZE, infile);
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioRefNum = outref;
pb.ioParam.ioBuffer = iobuffer;
pb.ioParam.ioReqCount = BLK_SIZE;
pb.ioParam.ioPosMode = fsAtMark;
pb.ioParam.ioPosOffset = 0;
myerr = PBWrite(&pb, FALSE);
if (myerr != noErr)
{
//Feedback("do_mac_copy: Error #%d on block #%d write.", myerr, blocks);
break;
}
}
if (residue)
{
RotateCursor(32);
DoYield();
memset(iobuffer, 0, BLK_SIZE);
fread(iobuffer, 1, residue, infile);
pb.ioParam.ioCompletion = 0;
pb.ioParam.ioRefNum = outref;
pb.ioParam.ioBuffer = iobuffer;
pb.ioParam.ioReqCount = residue;
pb.ioParam.ioPosMode = fsAtMark;
pb.ioParam.ioPosOffset = 0;
myerr = PBWrite(&pb, FALSE);
//if (myerr != noErr)
//Feedback("do_mac_copy: Error #%d on residue write.", myerr);
}
}